<?php

namespace Largezhou\Admin\Form;

use Largezhou\Admin\Components\Component;

class Field extends Component
{
    protected $name = '';

    /**
     * html中元素的宽度.
     *
     * @var string
     */
    protected $width = '420px';
    protected $label = '';
    protected $value = null;
    protected $required = false;
    protected $default = null;

    /**
     * @var Form
     */
    protected $form = null;

    public function __construct(string $fieldName, ...$args)
    {
        $this->name = $fieldName;
        $this->label = $this->formatLabel($args);
    }

    protected function formatLabel(array $args = []): string
    {
        return isset($args[0])
            ? $args[0]
            : str_replace(['.', '_'], ' ', ucfirst($this->name()));
    }

    public function variables(): array
    {
        $settings = [
            'name'      => $this->name(),
            'label'     => $this->label(),
            'value'     => $this->value(),
            'width'     => $this->width(),
            'component' => $this->component(),
            'props'     => $this->props(),
            'required'  => $this->required,
        ];

        $settings = array_merge($settings, $this->extraSettings());

        return $settings;
    }

    protected function extraSettings(): array
    {
        return [];
    }

    public function name(): string
    {
        return $this->name;
    }

    public function label(): string
    {
        return $this->label;
    }

    /**
     * 设置(可用来设置默认值)或获取字段的值.
     *
     * @param null $value
     *
     * @return $this|string
     */
    public function value($value = null)
    {
        if (func_num_args() === 0) {
            return $this->getValue();
        }

        return $this->setValue($value);
    }

    public function getValue()
    {
        return $this->value ?? $this->getDefault();
    }

    /**
     * @param mixed $value
     *
     * @return $this
     */
    public function setValue($value)
    {
        $this->value = $value;

        return $this;
    }

    /**
     * 获取或者设置html元素的宽度.
     *
     * @param string $width
     *
     * @return $this|string
     */
    public function width($width = null)
    {
        if (is_null($width)) {
            return $this->width;
        }

        $this->width = $width;

        return $this;
    }

    /**
     * 表单提交来的数据,有时要经过组件的特殊处理
     *
     * @param $value
     *
     * @return mixed
     */
    public function prepare($value)
    {
        return $value;
    }

    /**
     * 设置该字段为必填
     *
     * @return $this
     */
    public function required()
    {
        $this->required = true;

        return $this;
    }

    public function setDefault($default)
    {
        $this->default = $default;

        return $this;
    }

    public function getDefault()
    {
        if ($this->default instanceof \Closure) {
            return call_user_func($this->default, $this->form);
        }

        return $this->default;
    }

    /**
     * 持有Form表单实例,用来访问一些设置.
     *
     * @param Form $form
     *
     * @return $this
     */
    public function setForm(Form $form)
    {
        $this->form = $form;

        return $this;
    }
}
